package com.qqtech.qquan.community.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.PinyingUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.community.dao.CommunityBaseDao;
import com.qqtech.qquan.community.dao.CommunityInfoDao;
import com.qqtech.qquan.community.enums.CommunityBaseStatusDictEnum;
import com.qqtech.qquan.community.model.CommunityBase;
import com.qqtech.qquan.community.model.CommunityBaseQuery;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.model.CommunityInfo;

@Service
public class CommunityBaseServiceImpl extends BaseServiceImpl<CommunityBase> implements CommunityBaseService {
	public static final Logger log = LoggerFactory.getLogger(CommunityBaseServiceImpl.class);

	@Resource
	private CommunityBaseDao communityBaseDao;

	@Resource
	private CommunityInfoDao communityInfoDao;

	@Override
	protected BaseDao<CommunityBase> getBaseDao() {
		return communityBaseDao;
	}

	@Override
	public CommunityBase handleDetail(Integer id) {
		communityInfoDao.addPv(id);
		CommunityBase communityBase = communityBaseDao.getById(id);
		return communityBase;
	}

	@Override
	public int addCommunityBase(CommunityBase communityBase) {
		communityBase.setStatus(CommunityBaseStatusDictEnum.PENDING.getCode());
		communityBase.setAuditAt(null);
		String chnSpell = PinyingUtil.hanziToPinyinNoSpace(communityBase.getName());
		communityBase.setChnSpell(chnSpell);
		communityBase.setIsShow(YesNoEnum.NO.getCode());
		int id = communityBaseDao.insert(communityBase);
		CommunityInfo communityInfo = new CommunityInfo();
		communityInfo.setId(id);
		communityInfo.setActivity(0);
		communityInfo.setActivityLW(0);
		communityInfo.setPv(0);
		communityInfoDao.insert(communityInfo);
		return id;
	}

	@Override
	public Page<CommunityBaseVo> queryDistancePage(CommunityBaseQuery query, Pageable pageable) {
		return communityBaseDao.queryDistancePage(query, pageable);
	}

	@Override
	public int hide(Integer id) {
		CommunityBase communityBase = new CommunityBase();
		communityBase.setId(id);
		communityBase.setStatus(CommunityBaseStatusDictEnum.INVALID.getCode());
		communityBase.setIsShow(YesNoEnum.NO.getCode());
		return communityBaseDao.updateById(communityBase);
	}

	@Override
	public int show(Integer id) {
		CommunityBase communityBase = new CommunityBase();
		communityBase.setId(id);
		communityBase.setStatus(CommunityBaseStatusDictEnum.NORMAL.getCode());
		communityBase.setIsShow(YesNoEnum.YES.getCode());
		return communityBaseDao.updateById(communityBase);
	}

	@Override
	public int handleAuditPass(Integer id) {
		CommunityBase communityBase = new CommunityBase();
		communityBase.setId(id);
		communityBase.setStatus(CommunityBaseStatusDictEnum.NORMAL.getCode());
		communityBase.setIsShow(YesNoEnum.YES.getCode());
		communityBase.setAuditAt(TimeUtil.now());
		return communityBaseDao.updateById(communityBase);
	}

	@Override
	public List<CommunityBaseVo> queryDistanceList(CommunityBaseQuery query, Pageable pageable) {
		return communityBaseDao.queryDistanceList(query, pageable);
	}

	@Override
	public boolean isCommunityCreator(Integer userId, Integer communityId) {
		CommunityBase community = new CommunityBase();
		community.setCreatorId(userId);
		community.setId(communityId);
		return communityBaseDao.queryCount(community) > 0;
	}

	@Override
	public boolean isCommunityAgent(Integer userId, Integer communityId) {
		CommunityBase community = new CommunityBase();
		community.setAgentId(userId);
		community.setId(communityId);
		return communityBaseDao.queryCount(community) > 0;
	}

}